package org.example.short_link.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.example.short_link.project.common.convention.result.Result;
import org.example.short_link.project.common.convention.result.Results;
import org.example.short_link.project.dto.req.ShortLinkCreateReqDTO;
import org.example.short_link.project.dto.req.ShortLinkPageReqDTO;
import org.example.short_link.project.dto.req.ShortLinkUpdateReqDTO;
import org.example.short_link.project.dto.resp.ShortLinkCreateRespDTO;
import org.example.short_link.project.dto.resp.ShortLinkGroupCountQueryRespDTO;
import org.example.short_link.project.dto.resp.ShortLinkPageRespDTO;
import org.example.short_link.project.service.ShortLinkService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短链接控制层
 */
@RestController
@RequiredArgsConstructor
public class ShortLinkController {

    private final ShortLinkService shortLinkService;



    /**
     * 短链接跳转远链接
     * @param shorturi 短链接
     * @param request HTTP、请求
     * @param response HTTP 响应
     */
    @GetMapping("/{short-url}")
    public void restoreUrl(@PathVariable("short-url") String shorturi, HttpServletRequest request , HttpServletResponse response){
        shortLinkService.restoreUrl(shorturi,request,response);
    }

    /**
     * 创建短链接
     * @return
     */
    @PostMapping("/api/short-link/v1/group/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam) {
        return Results.success(shortLinkService.createShortLink(requestParam));

    }

    /**
     * 短链接分页查询
     * @param requestParam
     * @return
     */
    @GetMapping("/api/short-link/v1/group/page")
    public Result<IPage<ShortLinkPageRespDTO>> PageShortLink(ShortLinkPageReqDTO requestParam){
        return Results.success(shortLinkService.pageShortLink(requestParam));

    }

    /**
     * 查询分组下的短链接数量
     * @param requestParam
     * @return
     */
    @GetMapping("/api/short-link/v1/group/count")
    public Result<List<ShortLinkGroupCountQueryRespDTO>>ShortLinkCount(@RequestParam List<String> requestParam){

        return Results.success(shortLinkService.ShortLinkCount(requestParam));
    }

    /**
     * 修改短链接
     * @param requestParam
     * @return
     */
    @PostMapping("/api/short-link/v1/group/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam){
        shortLinkService.updateShortLink(requestParam);
        return Results.success();
    }













}
